package com.fysports.web.kit.umeditor;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.kit.commons.ContentTypeKit;
import com.fysports.web.kit.commons.CookiesKit;
import com.fysports.web.kit.commons.FileKit;
import com.fysports.web.model.system.SystemAttachmentFile;
import com.fysports.web.model.system.SystemUser;

import java.sql.SQLException;


/**
 * 文件上传下载公共controller
 * <p>
 * me.add("/umectr",UmeditorController.class); //上传下载up and down
 * <p>
 * Created by konbluesky
 * Date : 14-8-23 下午8:23
 * Project : JdmData_WebServer
 */

public class UmeditorController extends AbstractBaseController {
    private static Log logger = Log.getLog(UmeditorController.class);


    public void index() {
        renderText("非法访问");
    }

    /**
     * 接受umeditor上传方法
     *
     * @throws Exception
     */
    public void ueupload() throws Exception {

    }


    private void addSystemAttachmentFile(final Uploader up) throws Exception {
        /**
         * 上传失败不记录
         * 如果出现文件上传，但是数据库中无记录，则视为脏数据。
         */
        if (!up.getState().equals("SUCCESS"))
            return;
        final String path = up.getUrl().substring(0, up.getUrl().lastIndexOf("/") + 1);
        /**
         * 声明事务
         * 因两步数据库操作存在嵌套，所以不能使用 @Before(Tx.class) 这种方式
         */
        boolean tx = Db.tx(new IAtom() {
            @Override public boolean run() throws SQLException {
                SystemAttachmentFile saf = new SystemAttachmentFile();
                saf.set("srctype", "umedior-tips");  //上传源
                saf.set("path", path);               //路径 upload/20140824/ 不含文件名
                saf.set("filename", up.getFileName().replaceAll("\\..*", "")); //文件名 11181408870800858
                saf.set("suffix", up.getType());     //后缀 .jpg
                saf.set("filelength", up.getSize()); //
                saf.set("mimetype", ContentTypeKit.get(up.getType().replace(".", "")));
                saf.set(SystemAttachmentFile.COL_LIVEFLAG, 1);
                //                saf.set("createtime", DateTimeKit.getDate());
                boolean status = saf.save();

                String cookie = "";
                String useruuid = "";
                boolean status2 = false;
                if (status) {
                    useruuid = CookiesKit.getSystemUserUUID(UmeditorController.this.getRequest());//获取用户的uuid
                    try {
                        SystemUser.dao.addAttachmentFiles(useruuid, saf.getUUID());
                        status2 = true;
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                        logger.error("useruuid:" + useruuid + "||cookie:" + saf.getUUID(), e);
                        status2 = false;
                    }
                }
                //如果保存成功，则将文件保存到真实文件目录
                if (status && status2) {
                    try {
                        //此处用url而不是filename是因为
                        //url规则以生成upload/20140824/84791408810118438.jpg
                        //用这种方式直接copy
                        FileKit.moveTmpToReal(up.getUrl());
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return status && status2;
            }
        });

    }
}
